package com.good.msgsys.controller;

import com.good.msgsys.entity.Log;
import com.good.msgsys.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author junlong
 * @date 2021/1/5 03:10
 */
@Component
@Aspect
public class LogAop {
    @Autowired
    private LogService logService;

    private Date visitTime; // 开始时间
    private Class clazz; // 访问的类
    private Method method;// 访问的方法

    @Before("execution(* com.good.msgsys.controller.LoginController.login(..))")
    public void doBefore(JoinPoint jp) throws NoSuchMethodException {
        visitTime = new Date();// 当前时间就是开始访问的时间
        clazz = jp.getTarget().getClass(); // 具体要访问的类
        String methodName = jp.getSignature().getName(); // 获取访问的方法的名称
        Object[] args = jp.getArgs();// 获取访问的方法的参数

        // 获取具体执行的方法的Method对象
        if (args == null || args.length == 0) {
            method = clazz.getMethod(methodName); // 只能获取无参数的方法
        } else {
            Class[] classArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                classArgs[i] = args[i].getClass();
            }
            method = clazz.getMethod(methodName, classArgs);
        }
    }

    @After("execution(* com.good.msgsys.controller.LoginController.login(..))")
    public void doAfter(JoinPoint jp) {
        long time = new Date().getTime() - visitTime.getTime(); // 获取访问的时长

        String url = "";
        // 获取url
        /*if (clazz != null && method != null && clazz != LogAop.class) {
            //1.获取类上的@RequestMapping("/xxx")
            RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (classAnnotation != null) {
                String[] classValue = classAnnotation.value();
                //2.获取方法上的@RequestMapping(xxx)
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null) {
                    String[] methodValue = methodAnnotation.value();
                    url = classValue[0] + methodValue[0];

                    //获取当前操作的用户
                    *//*SecurityContext context = SecurityContextHolder.getContext();//从上下文中获了当前登录的用户
                    User user = (User) context.getAuthentication().getPrincipal();
                    String username = user.getUsername();*//*

                    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                    ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
                    HttpServletRequest request = sra.getRequest();

                    // 获取访问的ip
                    String ip = request.getRemoteAddr();

                    // 将日志相关信息封装到SysLog对象
                    Log log = new Log();
                    log.setExecutionTime(time); // 执行时长
                    log.setIp(ip);
                    log.setMethod("[类名] " + clazz.getName() + "[方法名] " + method.getName());
                    log.setUrl(url);
                    // log.setUser_name(username);
                    log.setVisitTime(visitTime);

                    // 调用Service完成操作
                    logService.add(log);
                }
            }
        }*/
        if (clazz != null && method != null && clazz != LogAop.class) {
            //2.获取方法上的@RequestMapping(xxx)
            RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
            if (methodAnnotation != null) {
                String[] methodValue = methodAnnotation.value();
                url = methodValue[0];

                //获取当前操作的用户
                    /*SecurityContext context = SecurityContextHolder.getContext();//从上下文中获了当前登录的用户
                    User user = (User) context.getAuthentication().getPrincipal();
                    String username = user.getUsername();*/

                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
                HttpServletRequest request = sra.getRequest();

                // 获取访问的ip
                String ip = request.getRemoteAddr();

                // 将日志相关信息封装到SysLog对象
                Log log = new Log();
                log.setExecutionTime(time); // 执行时长
                log.setIp(ip);
                log.setMethod("[类名] " + clazz.getName() + "[方法名] " + method.getName());
                log.setUrl(url);
                // log.setUser_name(username);
                log.setVisitTime(visitTime);

                // 调用Service完成操作
                logService.add(log);
            }
        }
    }
}
